<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          ref="input"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          ref="input"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间" prop="timeCategory">
        <el-select
          v-model="queryParams.timeCategory"
          placeholder="时间范围"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in timeCategory"
            :key="dict.value"
            :label="dict.name"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="walletList">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        :index="indexMethod"
        width="50px"
      />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户名称" align="center" prop="userName" />

      <template v-if="!queryParams.timeCategory">
        <el-table-column label="金额" align="center" prop="money" />
        <el-table-column label="余额" align="center" prop="afterAccount" />
        <el-table-column label="类型" align="center" prop="type">
          <template slot-scope="scope">
            {{ typeList[scope.row.type] }}
          </template>
        </el-table-column>
      </template>

      <template v-if="queryParams.timeCategory == 1">
        <el-table-column label="上月消费" align="center" prop="lastMonthConsumption" />
        <el-table-column label="本月消费" align="center" prop="thisMonthConsumption" />
        <el-table-column label="还需消费" align="center" prop="thisMonthNeedToConsume" />
        <el-table-column label="还需消费进度条" align="center" prop="thisMonthNeedToConsumePercentage" />
        <el-table-column label="本月可提现" align="center" prop="thisMonthCanBeWifthdrawn" />
        <el-table-column label="抽奖url" align="center" prop="thisMonthLuckDrawUrl" />
        <el-table-column label="是否进行年消费计算" align="center" prop="computionYearStatus">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.computionYearStatus == 0">否</el-tag>
            <el-tag type="success" v-if="scope.row.computionYearStatus == 1">是</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="账户状态" align="center" prop="status">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.status == 10">下架</el-tag>
            <el-tag type="success" v-if="scope.row.status == 20">上架</el-tag>
          </template>
        </el-table-column>
      </template>

      <template v-if="queryParams.timeCategory == 2">
        <el-table-column label="上年消费" align="center" prop="thisYearConsumption" />
        <el-table-column label="本年提现抽奖url" align="center" prop="thisYearLuckDrawUrl" />
        <el-table-column label="账户状态" align="center" prop="status">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.status == 10">下架</el-tag>
            <el-tag type="success" v-if="scope.row.status == 20">上架</el-tag>
          </template>
        </el-table-column>
      </template>

      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { history, historyMonth, historyYear } from "@/api/system/wallet";
import { mapGetters } from "vuex";
import { getToken } from "@/utils/auth";
import store from "@/store";

export default {
  name: "Goods",
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        userPhone: undefined,
        timeCategory: undefined
      },

      timeCategory: [
        {
          value: '1',
          name: '月'
        },
        {
          value: '2',
          name: '年'
        },
      ],

      typeList: {
        1: "給賬戶加錢",
        2: "給賬戶減錢",
        3: "用户红包",
        6: "消费记录",
        7: "用户退款",
        8: "月抽奖返现",
        9: "提现明细",
        10: "拉新",
        11: "单商品退款",
      },

      walletList: [],
      total: 0,
      loading: false,
    };
  },

  created() {
    const roles = store.getters && store.getters.roles;
    if (roles.indexOf("admin") > -1) {
      this.isExpand = true;
    }
    this.getList();
  },
  watch: {
    'queryParams.timeCategory'() {
      this.getList();
    }
  },
  methods: {
    indexMethod(index) {
      return (
        index + 1 + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      );
    },
    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      let fn = null
      if(this.queryParams.timeCategory == 1) {
        fn = historyMonth
      } else if(this.queryParams.timeCategory == 2) {
        fn = historyYear
      } else {
        fn = history
      }
      fn(this.queryParams).then((response) => {
        this.walletList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodsVideo: undefined,
        goodsId: undefined,
      };
      console.log(this.form);
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    arrToStr(arr) {
      if (arr.length <= 0) return "";
      return arr
        .map((item) => {
          return item.response.url;
        })
        .join(",");
    },
    strToArr(str) {
      if (str == "") return [];
      return str.split(",").map((item) => {
        return {
          url: item,
          response: {
            url: item,
          },
        };
      });
    },
  },
};
</script>
<style scoped>
</style>
